
import 'package:huxiaodui/api/model/alarm_model.dart';
import 'package:huxiaodui/utils/funs.dart';
import 'package:huxiaodui/view/index/map_page/map_controller.dart';
import 'package:huxiaodui/view/pop_pages/alarm_info_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:intl/intl.dart';


class AlarmMarker extends StatelessWidget {
  const AlarmMarker({super.key, required this.alarm});
  final AlarmModel alarm;

  @override
  Widget build(BuildContext context) {
    button({required IconData icon, required Function onTap}) {
      return InkWell(
        onTap: () => onTap(),
        child: Icon(
          icon,
          color: Theme.of(context).appBarTheme.foregroundColor!,
        ),
      );
    }

    line({required String text, double? maxWidth, double? fontSize}) {
      return Container(
        constraints: BoxConstraints(
          maxWidth: maxWidth ?? 300, // 设置最大宽度
        ),
        child: Text(
          text,
          overflow: TextOverflow.ellipsis,
          style: TextStyle(
            fontSize: fontSize ?? 12,
            color: Theme.of(context).appBarTheme.foregroundColor!,
          ),
        ),
      );
    }

    return Container(
      height: 140,
      width: 300,
      margin: EdgeInsets.all(30.0),
      alignment: Alignment.center,
      padding: const EdgeInsets.all(8),
      decoration: BoxDecoration(
        color: Colors.redAccent.withAlpha(1000),
        borderRadius: const BorderRadius.all(Radius.circular(16)),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withAlpha(90), // 阴影颜色
            offset: const Offset(2.0, 2.0), // 阴影偏移量
            blurRadius: 5.0, // 模糊半径
          ),
        ],
      ),
      child: Stack(
        children: [
          Positioned(
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  line(
                    text: "报警单位：${alarm.unit.name}",
                    maxWidth: 300,
                    fontSize: 14,
                  ),
                  line(
                    text: "时间：${DateFormat('yyyy-MM-dd HH:mm:ss').format(alarm.alarmAt)}",
                    fontSize: 12,
                  ),
                  // line(
                  //   text: "时间：${formatDateTime(alarm.alarmAt)}",
                  //   fontSize: 12,
                  // ),
                  line(
                    text: "报警类型: ${AlarmModel.typeList[alarm.type]}",
                    maxWidth: 300,
                    fontSize: 12,
                  ),
                  line(
                    text: alarm.unit.addr,
                    maxWidth: 300,
                    fontSize: 12,
                  ),
                  line(
                    text: "负责人：${alarm.unit.manager} (${alarm.unit.tel})",
                    maxWidth: 300,
                    fontSize: 12,
                  ),
                  line(
                    text: "辖区派出所：${alarm.unit.policeStation}",
                    maxWidth: 300,
                    fontSize: 12,
                  ),
                ],
              ),
            ),
          ),
          Positioned(
            right: 0,
            top: 0,
            child: button(
              icon: Icons.highlight_remove_outlined,
              onTap: () => MapController().hideMarkerInfo(),
            ),
          ),
          Positioned(
            right: 0,
            bottom: 0,
            child: button(
              icon: Icons.arrow_forward,
              onTap: () => MapController().moveMap(latLng: alarm.latLng!),
            ),
          ),
          Positioned(
            left: 0,
            top: 0,
            child: button(
              icon: Icons.menu,
              onTap: () => Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AlarmInfoPage(alarm: alarm)),
              ),
            ),
          ),
          Positioned(
            left: 0,
            bottom: 0,
            child: button(
                icon: Icons.add_location_alt_outlined,
                onTap: (){
                  if(alarm.latLng!=null || alarm.unit.latLng!=null) {
                    MapController().moveCenter(alarm.latLng ?? alarm.unit.latLng!);
                  }else{
                    EasyLoading.showError("没有定位消息！");
                  }
                }
            ),
          ),
        ],
      ),
    );
  }
}
